package com.leon.learnenginespace.drools.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * 运算工具类
 *
 * @author DJ032474
 */

@Slf4j
@Configuration
public class ModelUtils {
    /**
     * 模式运算处理
     *
     * @param filedPath
     * @param body
     * @param value
     * @param operationType
     * @return
     */
    public static Boolean operation(String filedPath, JSONObject body, String value, String operationType) {
        if (body == null) {
            log.warn("模式匹配，词条体为空");
            return false;
        }
        if (value == null) {
            log.warn("模式匹配，值为空");
            return false;
        }
        if (StringUtils.isEmpty(filedPath)) {
            log.warn("模式匹配，词条路径为空");
            return false;
        }
        String[] paths = RuleCoverJsonUtils.getStringArray(filedPath);
        int size = paths.length;
        boolean isArray = false;
        boolean isLeafNode = false;
        String key = null;
        String getKey = null;
        String filedValue = null;
        Object v = value;
        if ("containsOperation".equals(operationType)) {
            v = Arrays.asList(value.split(","));
        }
        JSONObject superJson = body;
        for (int i = 0; i < size; i++) {
            isLeafNode = (i + 1 == size);
            key = paths[i];
            isArray = RuleCoverJsonUtils.getIsArray(key);
            if (isArray && !isLeafNode) {
                log.warn("模式匹配，词条路径不支持数组");
                break;
            }
            getKey = RuleCoverJsonUtils.getPutKey(key, isArray);
            if (isLeafNode) {
                filedValue = superJson.getString(getKey);
                return OperationUtils.operationTowValue(v, filedValue, operationType, null);
            }
            superJson = body.getJSONObject(getKey);
            if (superJson == null) {
                break;
            }
        }
        return false;
    }

    /**
     * 词条运算
     *
     * @param filedPath
     * @param body
     * @return
     */
    public static Boolean singleOperation(String filedPath, JSONObject body, String operationType) {
        if (body == null) {
            log.warn("模式匹配，词条体为空");
            return false;
        }
        if (StringUtils.isEmpty(filedPath)) {
            log.warn("模式匹配，词条路径为空");
            return false;
        }
        String[] paths = RuleCoverJsonUtils.getStringArray(filedPath);
        int size = paths.length;
        boolean isArray = false;
        boolean isLeafNode = false;
        String key = null;
        String getKey = null;
        JSONObject superJson = body;
        for (int i = 0; i < size; i++) {
            isLeafNode = (i + 1 == size);
            key = paths[i];
            isArray = RuleCoverJsonUtils.getIsArray(key);
            if (isArray && !isLeafNode) {
                log.warn("模式匹配，词条路径不支持数组");
                break;
            }
            getKey = RuleCoverJsonUtils.getPutKey(key, isArray);
            if (isLeafNode) {
                return OperationUtils.singleOperation(superJson.get(getKey), operationType, null);
            }
            superJson = body.getJSONObject(getKey);
            if (superJson == null) {
                break;
            }
        }
        return false;

    }

}
